﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TravelLink.DAL;

namespace TravelLink.WebApp.Admin
{
    public partial class AdminAdmin : System.Web.UI.Page, ICallbackEventHandler
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.CheckResponse("8001");
        }

        protected void ldsAdmin_Inserting(object sender, LinqDataSourceInsertEventArgs e)
        {
            var obj = (TL_Admin)e.NewObject;
            obj.Idx = Guid.NewGuid();
            obj.Password= System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("123456", "MD5").ToLower();
        }

        private string CallBackValue = string.Empty;

        #region ICallbackEventHandler 成员

        public string GetCallbackResult()
        {
            return CallBackValue;
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            try
            {
                string[] eArg = eventArgument.Split(',');
                int idx = int.Parse(eArg[0]);
                string password = eArg[1];
                TL_Admin obj = (TL_Admin)this.ASPxGridView1.GetRow(idx);
                obj.Password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5").ToLower();
                var dc = DAL.DataContextFactory.Create();
                var dcobj = dc.TL_Admin.Where(t => t.Idx == obj.Idx).FirstOrDefault();
                dcobj.Password = obj.Password;
                dc.SubmitChanges();
                CallBackValue = "";
            }
            catch (Exception ex)
            {
                this.CallBackValue = ex.Message;
            }
        }

        #endregion

        protected void ASPxGridView1_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
        {
            if (e.NewValues["UserCode"]==null||e.NewValues["UserCode"].ToString().Trim() == "ADMINISTRATOR" || e.NewValues["UserCode"].ToString() == string.Empty || e.NewValues["UserCode"].ToString().IndexOf(',')>-1)
            {
                e.RowError = "登录名不能为空并且不能包含逗号";
            }
        }

        protected void abtnExport_Click(object sender, EventArgs e)
        {
            this.ASPxGridViewExporter1.WriteXlsToResponse(true);
        }
    }
}